local jinghe = fk.CreateSkill{
    name = "SP_heg__jinghe",
}

Fk:loadTranslationTable{
  ["SP_heg__jinghe"] = "经合",
  [":SP_heg__jinghe"] = "出牌阶段限一次，你可展示至多X张牌名各不同的手牌并选择等量有明置武将牌的角色，从“写满技能的天书”随机展示X个技能，这些角色依次选择并"..
  "获得其中一个技能，直到你下回合开始 （X为你的体力上限）。",

  ["#SP_heg__jinghe"] = "经合：展示至多体力上限张牌名各不同的手牌，令等量的角色获得技能",
  ["#SP_heg__jinghe-choice"] = "经合：选择你要获得的技能",

  ["$SP_heg__jinghe1"] = "大哉乾元，万物资始。",
  ["$SP_heg__jinghe2"] = "无极之外，复无无极。",
}

local H = require "packages.ol_hegemony.util"

jinghe:addEffect("active",{
  anim_type = "support",
  min_card_num = 1,
  min_target_num = 1,
  prompt = "#SP_heg__jinghe",
  can_use = function(self, player)
    return player:usedSkillTimes(jinghe.name, Player.HistoryPhase) == 0 and not player:isKongcheng()
  end,
  card_filter = function(self, player, to_select, selected)
    if #selected < player.maxHp and Fk:currentRoom():getCardArea(to_select) == Player.Hand then
      if #selected == 0 then
        return true
      else
        return table.every(selected, function(id) return Fk:getCardById(to_select).trueName ~= Fk:getCardById(id).trueName end)
      end
    end
  end,
  target_filter = function(self, player, to_select, selected, selected_cards)
    return #selected < #selected_cards and H.getGeneralsRevealedNum(to_select) > 0
  end,
  feasible = function (self, player, selected, selected_cards)
    return #selected > 0 and #selected == #selected_cards
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    room:setPlayerMark(player, "SP_heg__jinghe_used", 1)
    player:showCards(effect.cards)
    room:sortByAction(effect.tos)
    local num = 0 + #effect.tos
    local skills = table.random({"SP_heg__leiji", "yinbingn", "huoqi", "guizhu", "xianshou", "lundao", "guanyue", "yanzhengn"}, num)
    local selected = {}
    for _, p in ipairs(effect.tos) do
      if not p.dead then
        local choices = table.filter(skills, function(s) return not p:hasSkill(s, true) and not table.contains(selected, s) end)
        if #choices > 0 then
          local choice = room:askToChoice(p,{
            choices = choices,
            skill_name = jinghe.name,
            prompt = "#SP_heg__jinghe-choice:::"..#skills,
            detailed = true,
            all_choices = skills,
          })
          room:setPlayerMark(p, jinghe.name, choice)
          table.insert(selected, choice)
          room:handleAddLoseSkills(p, choice, nil, true, false)
        end
      end
    end
  end,
})

jinghe:addEffect(fk.TurnStart,{
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player:getMark("SP_heg__jinghe_used") > 0
      end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:setPlayerMark(player, "SP_heg__jinghe_used", 0)
        for _, p in ipairs(room.alive_players) do
          if p:getMark("SP_heg__jinghe") ~= 0 then
            local skill = p:getMark("SP_heg__jinghe")
            room:setPlayerMark(p, "SP_heg__jinghe", 0)
            room:handleAddLoseSkills(p, "-"..skill, nil, true, false)
          end
        end
      end,
})

jinghe:addEffect(fk.BuryVictim,{
    mute = true,
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        return target == player and player:getMark("SP_heg__jinghe_used") > 0
      end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        room:setPlayerMark(player, "SP_heg__jinghe_used", 0)
        for _, p in ipairs(room.alive_players) do
          if p:getMark("SP_heg__jinghe") ~= 0 then
            local skill = p:getMark("SP_heg__jinghe")
            room:setPlayerMark(p, "SP_heg__jinghe", 0)
            room:handleAddLoseSkills(p, "-"..skill, nil, true, false)
          end
        end
      end,
})

return jinghe